package com.lys.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.eduservice.client.VodClient;
import com.lys.eduservice.entity.*;
import com.lys.eduservice.entity.frontVo.CourseFrontVo;
import com.lys.eduservice.entity.frontVo.CourseWebVo;
import com.lys.eduservice.entity.vo.CourseInfoVo;
import com.lys.eduservice.entity.vo.CoursePublishVo;
import com.lys.eduservice.mapper.EduCourseMapper;
import com.lys.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lys.servicebase.exceptionhandler.GuliException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lys
 * @since 2022-06-29
 */
@Service
@Slf4j
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private VodClient vodClient;


    @Override
    @Transactional
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //添加课程信息 edu_course
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int ret = baseMapper.insert(eduCourse);

        if (ret==0){
            throw new GuliException(20001,"添加失败");
        }
        //获取添加成功后id
        String courseId =eduCourse.getId();
        log.info(courseId);

        //添加课程简介edu_course_description
        EduCourseDescription eduCourseDescription =new EduCourseDescription();
        eduCourseDescription.setId(courseId);
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.save(eduCourseDescription);
        return courseId;
    }

    @Override
    public CourseInfoVo queryCourseById(String courseId) {
        //查询课程简介
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        CourseInfoVo courseInfoVo =new CourseInfoVo();
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        //查询课程信息
        EduCourse eduCourse = this.getById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        return courseInfoVo;
    }

    @Override
    public void updateCourseById(CourseInfoVo courseInfoVo) {
        //修改课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(courseInfoVo.getId());
        boolean flag = eduCourseDescriptionService.updateById(eduCourseDescription);
        if (!flag){
            throw new GuliException(20001,"修改课程简介失败");
        }
        //修改课程信息
        EduCourse eduCourse =new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int ret = baseMapper.updateById(eduCourse);
        if (ret==0){
            throw new GuliException(20001,"修改课程信息失败");
        }
    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public int deleteCourseById(String id) {

        //删除小结视频
        LambdaQueryWrapper<EduVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getCourseId,id);
        queryWrapper.select(EduVideo::getVideoSourceId);
        List<EduVideo> eduVideoList = eduVideoService.list(queryWrapper);
        //遍历并获取小结视频id
        List<String> videoIds = new ArrayList<>();
        for (EduVideo eduVideo : eduVideoList) {
            if (!StringUtils.isEmpty(eduVideo.getVideoSourceId())){
                videoIds.add(eduVideo.getVideoSourceId());
            }
        }
        //根据多个视频id删除多个视频
        if(videoIds.size()>0) {
            vodClient.removeAlyVideoByIds(videoIds);
        }

        //删除小节
        LambdaQueryWrapper<EduVideo> videoQueryWrapper = new LambdaQueryWrapper<>();
        videoQueryWrapper.eq(EduVideo::getCourseId,id);
        eduVideoService.remove(videoQueryWrapper);



        //删除章节
        LambdaQueryWrapper<EduChapter> chapterQueryWrapper = new LambdaQueryWrapper<>();
        chapterQueryWrapper.eq(EduChapter::getCourseId,id);
        eduChapterService.remove(chapterQueryWrapper);
        //删除简介
        eduCourseDescriptionService.removeById(id);
        //删除课程
        int ret = baseMapper.deleteById(id);
        return ret;
    }


    //1 条件查询带分页查询课程
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        LambdaQueryWrapper<EduCourse> wrapper = new LambdaQueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq(EduCourse::getSubjectParentId,courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq(EduCourse::getSubjectId,courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc(EduCourse::getViewCount);
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc(EduCourse::getGmtCreate);
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc(EduCourse::getPrice);
        }

        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }

    //根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }



}
